﻿using UnityEngine;

[ExecuteInEditMode]
public class BhvCircle2D : MonoBehaviour
{
    public float radius = 5f;
    public float offsetAngle = 0;
    public bool lookAtToPivot;

    // Start is called before the first frame update
    void Start()
    {

    }



    // Update is called once per frame
    void Update()
    {
        float angle = (360 / this.transform.childCount);
        float startAngle = offsetAngle;

        for (int i = 0; i < this.transform.childCount; i++)
        {
            var child = this.transform.GetChild(i);
            float a = startAngle + angle * i;
            float x = Mathf.Cos(a * Mathf.Deg2Rad) * radius;
            float y = Mathf.Sin(a * Mathf.Deg2Rad) * radius;
            child.transform.localPosition = new Vector3(x, 0, y);

            float resAgnle = Vector3.SignedAngle(Vector3.forward, child.transform.localPosition, Vector3.up);

            if (lookAtToPivot)
            {
                child.transform.localEulerAngles = new Vector3(0, resAgnle, 0);
            }
            else
            {
                child.transform.localEulerAngles = Vector3.zero;
            }
        }

    }
}
